package com.winning.sx.microframework.service.base.impl;

import com.winning.sx.microframework.common.config.Config;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.common.util.SqlTool;
import com.winning.sx.microframework.dao.EasyDao;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.service.base.MessageService;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private EasyDao dao;

    @Override
    public Void saveMessages(Map<String, String> param) throws CustomException {
        try {
            Map message = new HashMap();
            message.put("staffId", param.get("staffId").toString());
            message.put("type", Constains.MESSAGE_WX);
            message.put("template", param.get("template"));
            if(param.get("openId")!=null)
                message.put("openId", param.get("openId"));
            message.put("sendContext", getWxContent(param.get("template"),param));
            message.put("state", Constains.MESSAGE_NEW);
            dao.insert("BASE_Message", message);
        } catch (SQLException e) {
            throw new CustomException(-1, "系统错误");
        }
        return null;
    }


    public static String getWxContent(String type, Map<String, String> param) {
        String template = "";
        if(param.get("remark")==null){
            param.put("remark","");
        }
        switch (type) {
            case Constains.MESSAGE_TEMPLATE_1:
                template = "{0}@#@{1}@#@{2}@#@{3}@#@{4}";
                String content1[]={param.get("type").toString(),
                        param.get("orgName").toString(),
                        param.get("deptName").toString(),
                        param.get("name").toString(),
                        param.get("tel").toString()};
                String contents=MessageFormat.format(template, content1);
                String first=param.get("first").toString();
                String remark=param.get("remark").toString();
                JSONObject data=new JSONObject();
                data.put("first",first);
                data.put("remark",remark);
                data.put("url", Config.domainUrl+"/edetail/"+param.get("eCode").toString()+"?flowCode="+param.get("mFlowCode").toString());
                data.put("content",contents);
                return data.toString();

            case Constains.MESSAGE_TEMPLATE_2:
                template = "{0}@#@{1}@#@{2}@#@{3}";
                String content2[]={param.get("orgName").toString(),
                        param.get("eCode").toString(),
                        param.get("mFlowCode").toString(),
                        param.get("time").toString()};
                String contents2=MessageFormat.format(template, content2);
                String first2=param.get("first").toString();
                String remark2=param.get("remark").toString();
                JSONObject data2=new JSONObject();
                data2.put("first",first2);
                data2.put("remark",remark2);
                data2.put("url",Config.domainUrl+"/edetail/"+param.get("eCode").toString()+"?flowCode="+param.get("mFlowCode").toString());
                data2.put("content",contents2);
                return data2.toString();

            case Constains.MESSAGE_TEMPLATE_3:
                template = "{0}@#@{1}@#@{2}@#@{3}@#@{4}";
                String content3[]={param.get("mFlowCode").toString(),
                        param.get("type").toString(),
                        param.get("orgName").toString(),
                        param.get("name").toString(),
                        param.get("tel").toString()};
                String contents3=MessageFormat.format(template, content3);
                String first3=param.get("first").toString();
                String remark3=param.get("remark").toString();
                JSONObject data3=new JSONObject();
                data3.put("first",first3);
                data3.put("remark",remark3);
                data3.put("url",Config.domainUrl+"/edetail/"+param.get("eCode").toString()+"?flowCode="+param.get("mFlowCode").toString());
                data3.put("content",contents3);
                return data3.toString();
            case Constains.MESSAGE_TEMPLATE_4:
                template = "{0}@#@{1}@#@{2}@#@{3}";
                String content4[]={param.get("name").toString(),
                        param.get("tel").toString(),
                        param.get("time").toString(),
                        param.get("result").toString()};
                String contents4=MessageFormat.format(template, content4);
                String first4=param.get("first").toString();
                String remark4=param.get("remark").toString();
                JSONObject data4=new JSONObject();
                data4.put("first",first4);
                data4.put("remark",remark4);
                data4.put("url",Config.domainUrl+"/edetail/"+param.get("eCode").toString()+"?flowCode="+param.get("mFlowCode").toString());
                data4.put("content",contents4);
                return data4.toString();
            default:
                return null;
        }
    }

    @Test
    public  void t(){
       String template = "{0}@#@{1}";
        String content5[]={"123","abc"};
        String contents5=MessageFormat.format(template, content5);
        String first5="恭喜您";
        String remark5="";
        JSONObject data5=new JSONObject();
        data5.put("first",first5);
        data5.put("remark",remark5);
        data5.put("content",contents5);
        System.out.println(data5.toString());
    }
}
